home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 1995 November / Macworld Nov ’95.toast / Developers / Selection ƒ 2.5 / $16 < prev    next >
Encoding:
Text File  |  1994-11-06  |  1.8 KB  |  91 lines  |  [TEXT/MSET]

  1. \ 28Oct94 dbh removed addr: references
  2.  
  3. (*
  4. Class $16 is a dictionary-based simple string class whose length may vary,
  5. but the maximum length is 16.
  6.  
  7. $16's are nice for use as string ivars, or if you want a persistent string
  8. object in the dictionary (no handles here so we don't need to do a new:
  9. and restore the data at each runtime).
  10.  
  11. *)
  12.  
  13.  
  14. :class $16 super{ object }
  15.     byte length
  16.     16 bytes text
  17.  
  18. :m limit:  ( -- lim )
  19.     16 ;m
  20.  
  21. :m size:  \ ( -- len)
  22.     get: length ;m        \ addr: will get the address of the indexed Width
  23.                             \ here we use width area to store the string length
  24.  
  25. private        \ private because we should never do this directly
  26.  
  27.     :m setsize:  \ ( len --)
  28.         dup  limit: self > abort" No more room in $16."
  29.         put: length
  30.         ;m
  31. public
  32.  
  33. :m addr:    \ ( -- addr)  \ redefine to give us the indexed data area
  34.     text ;m
  35.  
  36. :m put:  { addr len -- }
  37.     len setsize: self
  38.     addr  ( src)  addr: self ( dest) len ( cnt) cmove ;m
  39.  
  40. :m get: ( -- addr len )
  41.     addr: self  size: self ;m
  42.  
  43. :m print:
  44.     get: self type ;m
  45.  
  46. :m clear:
  47.     0 setsize: self ;m
  48.  
  49. :m classinit:
  50.     clear: self ;m
  51.  
  52. :m put$:  { $ptr -- }
  53.     $ptr 1 +  $ptr c@  put: self ;m
  54.  
  55. :m get$: \ ( -- $ptr )
  56.     length ;m
  57.  
  58. :m add: { addr len \ $len -- }
  59.     size: self -> $len
  60.     len $len + setsize: self
  61.     addr ( src) addr: self $len +  ( dest) len ( cnt) cmove ;m
  62.  
  63. :m add$: { $ptr -- }
  64.     $ptr 1 +  $ptr c@ add: self ;m
  65.  
  66. :m uc:  ( -- ) \ converts to upper case and does not get it
  67.     get: self upper ;m
  68.  
  69. :m +:  ( c -- ) \ appends a char to the end of the string
  70.     pad c! pad 1 add: self ;m
  71.  
  72. :m clip:  { n -- }  \ remove n characters from end of string
  73.                     \ if n is too large, string is just cleared with no error
  74.     size: self n - 0 max setsize: self ;m
  75.  
  76. ;class
  77.  
  78. endload
  79.  
  80. *** EXAMPLE USE
  81.  
  82. 7 $16 jj
  83. print: jj
  84. size: jj .
  85. 3 clip: jj
  86. " hello" put: jj
  87. " ff" add: jj
  88. uc: jj type
  89. 32 +: jj
  90.  
  91.